﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Instructor.Models;
using PagedList;
namespace OT.Areas.Instructor.Controllers
{
    public class TopicController : Controller
    {
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
       
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            int UserID = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value); ;
            }
            else
            {
                TempData["notice"] = "Time Out ,Please Login";
                Response.Redirect("~/Home");
            }
            List<Topics> topic = (from c in OTDb.Topics
                                  join sc in OTDb.Subjects
                                  on c.SubjectID equals sc.SubjectID into output
                                  from j in output.DefaultIfEmpty()
                                  where c.UserID.Value.Equals(UserID)
                                  select new Topics
                                  {
                                      TopicID = c.TopicID,
                                      TopicName = c.TopicName,
                                      LanguageID = LanguageID,
                                      Description = c.Description,
                                      SubjectName = j.SubjectName,
                                      CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                      UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue
                                  }
                                                        ).OrderBy(o => o.TopicName).ToList<Topics>(); ;
            if (!string.IsNullOrEmpty(searchString))
                topic = topic.Where(r => r.TopicName.ToLower().Contains(searchString.ToLower())).ToList();
            int recount = topic.Count;
            topic = topic.Skip<Topics>(pgindex * (pageNumber - 1))
                .Take<Topics>(pgindex).ToList<Topics>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = topic,
                RecordCount = recount
            });
        }

        public ActionResult Create()
        {
            Topics topic = null;
            try
            {
                topic = new Topics();
                int[] ids = { 1, 2 };
                var user = (from dt in OTDb.Users
                            where ids.Contains((int)dt.UserTypeID)
                            select dt).ToList();
                topic.Users = user;
                var sub = OTDb.Subjects.Where(s=>s.Status==true).ToList();
                topic.Subjects = sub;
                var cat = OTDb.Categories.Where(c=>c.Status==true).ToList();
                topic.category = cat;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(topic);
        }
        [HttpPost]
        public string Create(String TopicName, int SubjectID, String Description, Boolean Status)
        {
            int UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            string result = "";
            Topic topc = null;
            try
            {
                if (ModelState.IsValid)
                {
                    topc = new Topic();
                    topc.TopicName = TopicName;
                    topc.Description = Description;
                    topc.Status = Status;
                    topc.LanguageID = LanguageID;
                    topc.SubjectID = SubjectID;
                    topc.UserID = UserID;
                    topc.CreatedDate = DateTime.Now;
                    topc.UpdatedDate = DateTime.Now;
                    OTDb.Topics.Add(topc);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.Topics.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        public ActionResult Edit(int TopicID)
        {
            Topics topc = null;
            try
            {
                var categry = OTDb.Topics.Where(r => r.TopicID == TopicID).SingleOrDefault();
                if (categry != null)
                {
                    int id = (int)categry.SubjectID;
                    topc = new Topics();
                    topc.CategoryID = GetCategory(id);
                    topc.TopicID = categry.TopicID;
                    topc.TopicName = categry.TopicName;
                    topc.SubjectID = categry.SubjectID;
                    topc.Description = categry.Description;
                    topc.UserID = categry.UserID;
                    topc.Status = (bool)categry.Status;
                    topc.LanguageID = LanguageID;
                    var user = OTDb.Users.ToList();
                    topc.Users = user;
                    var sub = OTDb.Subjects.ToList();
                    topc.Subjects = sub;
                    var cat = OTDb.Categories.ToList();
                    topc.category = cat;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(topc);
        }

        [HttpPost]
        public string Edit(int TopicID, String TopicName, String Description, Boolean Status, int SubjectID)
        {
            string result = "";
            int UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            try
            {
                if (ModelState.IsValid)
                {
                    var topc = OTDb.Topics.Where(r => r.TopicID == TopicID).SingleOrDefault();
                    if (topc != null)
                    {
                        topc.TopicID = TopicID;
                        topc.TopicName = TopicName;
                        topc.SubjectID = SubjectID;
                        topc.Description = Description;
                        topc.UserID = UserID;
                        topc.Status = Status;
                        topc.LanguageID = LanguageID;
                        topc.UpdatedDate = DateTime.Now;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }

        public ActionResult Delete(int TopicID)
        {
            Topics topc = null;
            try
            {
                var categry = OTDb.Topics.Where(r => r.TopicID == TopicID).SingleOrDefault();
                if (categry != null)
                {
                    topc = new Topics();
                    topc.TopicName = categry.TopicName;
                    topc.SubjectID = categry.SubjectID;
                    topc.Description = categry.Description;
                    topc.UserID = categry.UserID;
                    topc.LanguageID = LanguageID;
                    topc.Status = (bool)categry.Status;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(topc);
        }

        [HttpPost]
        public string Delete(int TopicID, string IsDelete)
        {
            string result = "";
            try
            {
                var cat = OTDb.Topics.Where(r => r.TopicID == TopicID).SingleOrDefault();
                if (cat != null)
                {
                    OTDb.Topics.Remove(cat);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }

        private IList<Subject> GetSubject(int CategoryID)
        {
            return OTDb.Subjects.Where(m => m.CategoryID == CategoryID).ToList();
        }
        private int GetCategory(int CategoryID)
        {
            int rec;
            var query1 = (from c in OTDb.Subjects
                          orderby c.CategoryID ascending
                          where c.SubjectID == CategoryID
                          select c).SingleOrDefault();
            rec = (int)query1.CategoryID;
            return rec;
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadSubject(string CategoryID)
        {
            var stateList = this.GetSubject(Convert.ToInt32(CategoryID));
            var stateData = stateList.Select(m => new SelectListItem()
            {
                Text = m.SubjectName,
                Value = m.SubjectID.ToString(),
            });
            return Json(stateData, JsonRequestBehavior.AllowGet);
        }

        private IList<Subject> GetSub(int CategoryID)
        {
            return OTDb.Subjects.Where(m => m.CategoryID == CategoryID).ToList();
        }
        private IList<Topic> GetTopic(int SubjectID)
        {
            return OTDb.Topics.Where(c => c.SubjectID == SubjectID).ToList();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadTopic(string SubjectID)
        {
            var CityList = this.GetTopic(Convert.ToInt32(SubjectID));
            var CityData = CityList.Select(c => new SelectListItem()
            {
                Text = c.TopicName,
                Value = c.TopicID.ToString(),
            });
            return Json(CityData, JsonRequestBehavior.AllowGet);
        }

    }
}
